package com.uestc.admin;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import com.uestc.hbase.MTable;
import com.uestc.util.TimeUtil8;

public class SystemLogAdmin implements AdminBase {

	private String columnName = "SystemLogColumns";
	private String tableName = "system-log";

	@Override
	public String getColumnName() {
		return columnName;
	}

	@Override
	public String getTableName() {
		return tableName;
	}

	@Override
	public String getInfo(int page, int limit, HttpServletRequest request) throws Throwable {
		return this.getDetailInfo(tableName, columnName, Arrays.asList(new String[] { "info" }), page, limit, request);
	}

	@Override
	public String getInfoWithCondition(int page, int limit, HashMap<String, String> conditions,
			HttpServletRequest request) throws Throwable {
		String beginTime = conditions.get("beginTime");
		String endTime = conditions.get("endTime");
		String errorType = conditions.get("errorType");
		conditions.clear();
		if (beginTime == null || beginTime.isEmpty() || endTime == null || endTime.isEmpty())
			return null;
		ArrayList<String> times = TimeUtil8.getDatesBetweenTowDates(beginTime, endTime);
		// 如果不包含errorType，则全部error加进去
		if (errorType == null || errorType.isEmpty()) {
			Field[] fields = ErrorType.class.getDeclaredFields();
			for (Field field : fields) {
				for (String time : times) {
					StringBuilder sb = new StringBuilder(field.get(null).toString());
					sb.append(";");
					sb.append(time);
					conditions.put(sb.toString() + MTable.FILTER_TYPE.ROW_FILTER, sb.toString());
				}
			}
		} else {
			for (String time : times) {
				StringBuilder sb = new StringBuilder(errorType);
				sb.append(";");
				sb.append(time);
				conditions.put(time + MTable.FILTER_TYPE.ROW_FILTER.toString(), sb.toString()); // 对于行键搜索，conditions的key作用不大
			}
		}

		return this.getDetailInfo(tableName, columnName, Arrays.asList(new String[] { "info" }), page, limit, request,
				"info", conditions, MTable.FILTER_TYPE.PREFIX_FILTER, MTable.FILTER_COMBINE.MUST_PASS_ONE);
	}

	/**
	 * 错误类型
	 * 
	 * @author lijiong
	 *
	 */
	static class ErrorType {
		public static final String DELETE_FAILED = "delete failed";
		public static final String REGISTER_FAILED = "register failed";
		public static final String LOGIN_FAILED = "login failed";
		public static final String UPLOAD_FAILED = "upload failed";
		public static final String DOWNLOAD_FAILED = "download failed";
		public static final String QUERYQUOTA_FAILED = "queryQuota failed";
		public static final String GENDOWNLOADSTRATEGY_FAILED = "genDownloadStrategy failed";
		public static final String REDUCEFLOW_FAILED = "reduceFlow failed";
		public static final String FINDPASSWORD_FAILED = "findPassword failed";
		public static final String SAVEFEEDBACK_FAILED = "saveFeedback failed";
		public static final String GENUPLOADSTRATEGY_FAILED = "genUploadStrategy failed";
		public static final String COMPLETEUSERINFO_FAILED = "completeUserInfo failed";
		public static final String UPLOADMETADATA_FAILED = "uploadMetaData_failed";
		public static final String DOWNLOADMETADATA_FAILED = "downloadMetaData_failed";
		public static final String DELETE = "delete";
	}

	@Override
	public String getDateColumnName() {
		return "date";
	}
}
